// mircoApp/manualOrder/manualOrder/manualOrder.js
const app = getApp();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    tabArr: [],
    active: 0,
    listInfo: {},
    page: 1,
    current_page: 0,
    isLoadMore: true,
    searchVal:'',
    AuthText:[],
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
  },
  searchMehtod() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      listInfo: []
    })
    this.getData();
  },
  resetGetData() {
    this.setData({
      page: 1,
      total_page: 0,
      isLoadMore: true,
      listInfo: []
    })
    this.getData();
  },
  tabsChange(e) {
    this.setData({
      active: e.detail.index,
      page: 1,
      total_page: 0,
      isLoadMore: true,
      listInfo: []
    })
    this.getData();
  },
  getData() {
    let urlStr = app.getNetAddresss("plugin.manual-order.frontend.order.index");
    app._getNetWork({
      url: urlStr,
      data: {
        order_type: this.data.tabArr[this.data.active].order_type,
        page: this.data.page,
        order_sn: this.data.searchVal
      },
      success: (response) => {
        let res = response.data;
        if (res.result === 1) {
          this.setData({
            listInfo: res.data.data,
            current_page: res.data.current_page,
            isLoadMore: true
          })
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  getMoreData() {
    this.data.isLoadMore = false; // 防止多次请求分页数据
    if (this.data.page >= this.data.total_page) {
      return;
    } else {
      this.setData({
        page: this.data.page + 1
      })
      let urlStr = app.getNetAddresss('plugin.manual-order.frontend.order.index');
      app._postNetWork({
        url: urlStr,
        data: {
          page: this.data.page,
          order_type: this.data.tabArr[this.data.active].order_type,
        },
        success: (resdata) => {
          let res = resdata.data
          if (res.result == 1) {
            this.data.isLoadMore = true;
            this.setData({
              listInfo: this.data.listInfo.concat(res.data.data)
            })
          } else {
            this.data.page = this.data.page - 1;
            this.isLoadMore = false
            wx.showToast({
              icon: 'none',
              title: res.msg,
              duration: 1500
            });
          }
        },
        fail: function (res) {
          console.log(res);
        }
      });
    }
  },
  getListBar() {
    let urlStr = app.getNetAddresss("plugin.manual-order.frontend.order.listBar");
    app._getNetWork({
      url: urlStr,
      success: (response) => {
        let res = response.data;
        if (res.result === 1) {
          console.log(res)
          this.setData({
            tabArr: res.data.bar,
            AuthText: res.data.auth,
          })
          this.getData();
        } else {
          wx.showToast({
            title: res.msg,
            icon: 'none',
            duration: 2000
          });
        }
      },
      fail: function (res) {
        console.log(res);
      }
    });
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {
    this.getListBar();
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {
    this.getMoreData();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage() {

  }
})